<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" @submit.native.prevent>
      <el-form-item label="关键字" prop="preId">
        <el-input v-model="queryParams.preId" placeholder="吉祥号" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['member:pre:add']">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="preList">
      <el-table-column label="序号" type="index" align="center" width="60" />
      <el-table-column label="吉祥号" prop="preId" align="center" class-name="small-padding fixed-width" />
      <el-table-column label="绑定电话" prop="phone" align="center" width="300" />
      <el-table-column label="昵称" prop="nickname" align="center" width="300" />
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['member:pre:edit']">绑定</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click.stop="handleDelete(scope.row)" v-hasPermi="['member:pre:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改角色配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item v-if="title != '绑定用户'" label="吉祥号" prop="preId">
          <el-input v-model.number="form.preId" placeholder="请输入吉祥号" />
        </el-form-item>
        <el-form-item v-else label="吉祥号" prop="preId">
          <el-input v-model.number="form.preId" :disabled="true" />
        </el-form-item>
        <el-form-item v-if="title == '绑定用户'" label="电话" prop="phone">
          <el-input v-model.number="form.phone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item v-if="title == '绑定用户'" label="昵称" prop="nickname">
          <el-input v-model.number="form.nickname" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item v-if="title == '绑定用户'" label="邀请码" prop="inviteCode">
          <el-input v-model.number="form.inviteCode" placeholder="请输入邀请码" />
        </el-form-item>
        <el-form-item v-if="title == '绑定用户' && !form.phone" label="密码" type="password" prop="password">
          <el-input v-model.number="form.password" placeholder="请输入密码" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPre, addPre, updatePre, delPre } from "@/api/member/auspicious.js";

export default {
  name: "Robot", //机器人管理
  data() {
    return {
      // 列表数据
      preList: [],

      dialogVisible: false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,

      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: this.global.pageSize,
        preId: undefined,
      },

      // 表单参数
      form: {},

      rules: {
        preId: [
          { required: true, message: "吉祥号不能为空" },
          { type: "number", message: "吉祥号必须为数字值" },
        ],
        phone: [
          { required: true, message: "电话不能为空" },
          { type: "number", message: "电话必须为数字值" },
          // { min: 11, max: 11, message: "电话号码长度不对" },
        ],
        nickname: [{ required: true, message: "昵称不能为空" }],
        inviteCode: [{ required: true, message: "推荐人不能为空" }],
        password: [{ required: true, message: "密码不能为空" }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 获取吉祥号列表 */
    getList() {
      this.loading = true;
      listPre(this.queryParams).then((response) => {
        // if (response.code == 200) {
        //   this.preList = response.data;
        //   this.preList.forEach((item) => {
        //     if (item.phone) {
        //       item.phone =
        //         item.phone.substring(0, 3) +
        //         "****" +
        //         item.phone.substr(item.phone.length - 4);
        //     }
        //   });
        //   this.total = response.total;
        // }
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.form = {
        preId: undefined,
      };
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增吉祥号";
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.title = "绑定用户";
      this.form.preId = row.preId;

      if (row.phone) {
        this.form.phone = row.phone;
        this.form.nickname = row.nickname;
        this.form.inviteCode = row.inviteCode;
        this.form.password = row.password;
      }

      this.open = true;
    },

    /** 提交按钮 */
    submitForm(from) {
      this.$refs[from].validate((valid) => {
        if (valid) {
          if (this.form.phone) {
            updatePre(this.form).then((response) => {
              if (response.code == 200) {
                this.$modal.msgSuccess("绑定成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addPre(this.form).then((response) => {
              if (response.code == 200) {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        } else {
          return false;
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm('是否确认删除吉祥号为"' + row.preId + '"的数据项？')
        .then(() => {
          delPre({ preId: row.preId }).then((response) => {
            if (response.code == 200) {
              this.$modal.msgSuccess("删除成功");
              this.open = false;
              this.getList();
            }
          });
        })
        .catch(() => { });
    },

  },
};
</script>
<style scoped>
</style>
